import router from "@/router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { ElMessageBox } from "element-plus";
import store from "~store";
import szdStoreSession from "../store/storeSession";
import szdStoreCookie from "../store/storeCookie";
import szdRouter from "./goRouter";
import { $m, $t } from "./globalConfig";
import { ssoLogin } from "../api/login";

//进度条
NProgress.configure({ showSpinner: false });
//白名单
const whiteList = ["/login"];
//路由拦截器
router.beforeEach((to, from, next) => {
  let token = szdStoreCookie.getToken();
  //检查是否是单点登录界面，要打开的页面是core/sso, 虚拟路由
  if (ssoLoginCheck(to, from, next, token)) {
    NProgress.start();
    if (token) {
      if (to.path === "/login") {
        next({ path: "/" });
        NProgress.done();
      } else {
        if (store.state.loginUser.user.userId === undefined) {
          store.dispatch("GetUserInfo").then(res => {
            if (res) {
              next({ ...to, replace: true });
            } else {
              store.dispatch("LogOut").then(() => {
                next({ path: "/" });
              });
            }
          });
        } else {
          if (!router.hasRoute(to.name)) {
            NProgress.done();
            return;
          }
          next();
        }
      }
    } else {
      // 没有token
      if (whiteList.indexOf(to.path) !== -1) {
        next(); // 在免登录白名单，直接进入
      } else {
        next(`/login`); // 否则全部重定向到登录页 next(`/login?redirect=${to.path}`)
        NProgress.done();
      }
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});

//单点登录检查
function ssoLoginCheck(to, from, next, token) {
  if (to.path !== "/core/sso") return true;
  const ssoToken = to.query.token;
  const ssoUserId = to.query.userId;
  if (ssoToken) {
    if (token) {
      //检查是否同一用户
      const loginUser = szdStoreSession.getCoreLogin();
      if (loginUser.user.userId === ssoUserId) {
        ssoLoginNext(to.query, false, next);
      } else {
        ElMessageBox.confirm($t("不同用户登录，登录信息将被重置, 是否继续?"), $t("提示"), {})
          .then(() => {
            ssoLoginNext(to.query, true, next);
          })
          .catch(() => {
            next("/index");
          });
      }
    } else {
      szdStoreCookie.setToken(to.query.token);
      next();
    }
  } else {
    $m("CORE_CLIENT.E017"); //单点登录未携带token信息，请联系对应系统技术人员
    next({ path: "/" });
  }
  return false;
}

//单点登录刷新登录有效期
function ssoLoginNext(query, next) {
  //将SSOToken转换为WebToken，同时刷新有效期
  ssoLogin({ userId: query.userId, token: query.token }).then(res => {
    if (res.code === 200) {
      szdStoreSession.removeCoreLogin();
      szdStoreCookie.setToken(res.token);
      store.dispatch("GetUserInfo").then(res => {
        if (res) {
          ssoLoginRedirect(query);
        } else {
          store.dispatch("LogOut").then(() => {
            $m("CORE_CLIENT.E015"); //单点登录token已过期,请重新登录
            next({ path: "/" });
          });
        }
      });
    } else {
      $m("CORE_CLIENT.E015"); //单点登录token已过期,请重新登录
      next({ path: "/" });
    }
  });
}

//单点登录重新定向
function ssoLoginRedirect(query) {
  const params = query;
  const routerCode = params.routerCode;
  delete params.token;
  delete params.userId;
  delete params.routerCode;
  szdRouter.goNav(routerCode, params);
}
